package com.thinkingstar.iads.test.urule.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.nutz.boot.tools.AsSpringBean;
import org.nutz.ioc.loader.annotation.IocBean;

import com.bstek.urule.model.ExposeAction;

import neu.sxc.expression.Expression;
import neu.sxc.expression.ExpressionFactory;

@IocBean
@AsSpringBean
public class HighspeedrailAction {

	public static List<Map<String,Object>> list = new ArrayList<>();


	@ExposeAction("保存List数据")
	public  void addlist(List<Map<String,Object>> listMap){
		list.addAll(listMap);
	}
	
	@ExposeAction("获取List数据")
	public  List<Map<String,Object>> getlist(){
		return list;
	}


	@ExposeAction("清空List")
	public  void cleanList(){
		list.clear();
	}
	
	@ExposeAction("打印List")
	public  void sysoList(){
		for (Map<String, Object> map : list) {
			System.out.println(map);
		}
	}
	
	
	@ExposeAction("获取高铁提入资料数据")
	public  List<Map<String,Object>> getInputData(String eng_id,String sjnr){
		List<Map<String,Object>> list = new ArrayList<>();
		Map<String,String> rmap = new HashMap<>();
		rmap.put("工区", "福清西");
		rmap.put("车间", "莆田（含工区）");
		int num = 20;
		Set<Entry<String, String>> entrySet = rmap.entrySet();
		for (Entry<String, String> entry : entrySet) {
			Map<String,Object> map = new HashMap<>();
			map.put("eng_id", "cb21b80fc129448f8e4f864e62928a50");
			map.put("sjnr", "高铁综合维修");
			map.put("maintain_organization_name",entry.getValue());
			map.put("maintain_organization_type", "综合维修"+entry.getKey());
			map.put("govern_mileage", num+=10);
			list.add(map);
		}
		return list;
	}



	@ExposeAction("获取变量表数据")
	public  List<Map<String,Object>> getVariableData(String eng_id,String sjnr,String name){
		String[] dy = {"供电定员","给排水定员","总定员"}; 
		String[] mj = {"供电面积","给排水面积","综合楼面积"};
		Map<String,String> rmap = new HashMap<>();
		rmap.put("工区", "福清西");
		rmap.put("车间", "莆田（含工区）");

		String[] strs = "定员".equals(name)?dy:mj;
		List<Map<String,Object>> list = new ArrayList<>();

		Set<Entry<String, String>> entrySet = rmap.entrySet();
		for (Entry<String, String> entry : entrySet) {
			for (String strsName : strs) {
				Map<String,Object> map = new HashMap<>();
				map.put("eng_id", "cb21b80fc129448f8e4f864e62928a50");
				map.put("sjnr", "高铁综合维修");
				map.put("maintain_organization_name",entry.getValue());
				map.put("maintain_organization_type", "综合维修"+entry.getKey());
				map.put("variable_name", strsName);
				map.put("value", null);
				map.put("zt", 0);
				list.add(map);
			}
		}
		return list;
	}



	@ExposeAction("获取公式")
	public  Map<String,Object> getFormula(Object name){
		Map<String,Object> rmap = new HashMap<>();
		if("供电定员".equals(name)){	
			rmap.put("eng_id", "cb21b80fc129448f8e4f864e62928a50");
			rmap.put("constant", "MD_X11303010001__GXLC,MD_X11303010012__JWDY");
			rmap.put("expression", "(0.63*MD_X11303010001__GXLC*1.07+0.3*MD_X11303010001__GXLC)*1.04+MD_X11303010012__JWDY");
			rmap.put("formula_name", "双线供电定员");
			rmap.put("variatee", "TL_D11303020024__SXGDDY");
		}else if("给排水定员".equals(name)){
			rmap.put("eng_id", "cb21b80fc129448f8e4f864e62928a50");
			rmap.put("constant", "MD_X11303010001__GXLC,MD_X11303010012__JWDY");
			rmap.put("expression", "0.1*MD_X11303010001__GXLC+MD_X11303010012__JWDY");
			rmap.put("formula_name", "双线给排水定员");
			rmap.put("variatee", "TL_D11303020024__DXJPSDY");
		}else if("供电面积".equals(name)){
			rmap.put("eng_id", "cb21b80fc129448f8e4f864e62928a50");
			rmap.put("constant", "TL_D11303020011__HYS,TL_D11303020011__CS,TL_D11303020011__ZBS,TL_D11303020011__DLZMJKS");
			rmap.put("expression", "TL_D11303020024__SXGDDY*6 +TL_D11303020011__HYS +TL_D11303020011__CS +TL_D11303020011__ZBS +TL_D11303020024__SXGDDY*1+TL_D11303020011__DLZMJKS");
			rmap.put("formula_name", "供电面积");
			rmap.put("variatee", "TL_D11303020024__DXGDGQMJ");
		}else if("给排水面积".equals(name)){
			rmap.put("eng_id", "cb21b80fc129448f8e4f864e62928a50");
			rmap.put("constant", "TL_D11303020011__HYS,TL_D11303020011__CS,TL_D11303020011__ZBS");
			rmap.put("expression", "TL_D11303020024__SXJPSDY*6 +(TL_D11303020011__HYS  +TL_D11303020011__CS +TL_D11303020011__ZBS)/2 +TL_D11303020024__SXJPSDY*1");
			rmap.put("formula_name", "给排水面积");
			rmap.put("variatee", "TL_D11303020024__SXJPSCJMJ");
		}
		return rmap;
	}


	//@ActionMethod(name="获取常量")
	//@ActionMethodParameter(names={"eng_id","常量名称"})
	public  String getConstant(String code_name){
		Map<String,String> rmap = new HashMap<>();
		rmap.put("JWDY", "10");

		rmap.put("HYS", "5");
		rmap.put("CS",  "5");
		rmap.put("ZBS",  "10");
		rmap.put("DLZMJKS", "10");
		return  rmap.get(code_name);
	}


	//@ActionMethod(name="获取变量")
	//@ActionMethodParameter(names={"变量名称"})
	public  String getVariable(Object maintain_organization_name,Object name){
		for (Map<String, Object> map : list) {
			if(map.get("maintain_organization_name").equals(maintain_organization_name)&&name.equals(map.get("variable_name")))
				return map.get("value").toString();
		}
		return null;
	}

	//获取提入资料的值
	public  String getInputDataNum(Object name){
		List<Map<String, Object>> inputData = getInputData("","");
		for (Map<String, Object> map : inputData) {
			if(map.get("maintain_organization_name").equals(name)){
				return map.get("govern_mileage").toString();
			}
		}
		return null;
	}

	//定员表与面积表  对应关系
	public Object getType(String name){
		Map<String,Object> map = new HashMap<>();
		map.put("供电面积", "供电定员");
		map.put("给排水面积", "给排水定员");
		return map.get(name);
	}




	/**
	 * 这里只针对高铁，如果有多个专业，扩展此方法
	 * @param name
	 * @return
	 */
	@ExposeAction("给公式赋值")
	public  Map<String,Object> getAssemble(Object name,Object strName,Object constant,Object expressionStr){
		Map<String,Object> map = new HashMap<>();
		//1.解析综合维修  公式
		ExpressionFactory factory = ExpressionFactory.getInstance();
		Expression exp = factory.getExpression(expressionStr.toString());
		Set<String> variableNames = exp.getVariableNames();//获取表达式里面的所有变量名和常量名
		//2.获取公式表每个常量名
		String[] constants = constant.toString().split(",");
		//3.区分变量和常量，分别赋值
		for(String vableName:variableNames){
			boolean flag = false;
			String vable = vableName;
			for(String cont: constants){
				if(vableName.equals(cont.trim())){
					flag = true;
					vable = cont;
					break;
				}
			}
			if(flag){//相同就是常量
				//获取code_name
				String[] split = vable.split("__");
				String code_name = split[split.length-1].trim();

				String value = getConstant(code_name);
				if(value==null){	//提入资料
					value = getInputDataNum(name);
				}
				map.put(vable.trim(), value);

			}else{  //不相同就是变量
				strName = getType(strName.toString());
				map.put(vable.trim(), getVariable(name,strName));
			}	

		}
		return map;
	}

	


	/**
	 * 计算
	 * @param map        表达式需要的变量名和值
	 * @param expression 表达式
	 * @return
	 */
	@ExposeAction("计算公式")
	public int getNumber(Map<String, Object> map,Object expressionStr){
		ExpressionFactory factory = ExpressionFactory.getInstance();
		Expression expression = factory.getExpression(expressionStr+";");
		//  赋值计算
		Set<Entry<String, Object>> entrySet = map.entrySet();
		for (Entry<String, Object> entry : entrySet) {
			expression.setVariableValue(entry.getKey() ,Double.parseDouble(entry.getValue().toString()));
		}
		// 执行表达式
		int number =  (int)Math.ceil(Float.valueOf(expression.evaluate().getNumberValue().toString()));
		return number;
	}


	
	public void main() {
		test01("定员");
		test01("面积");
	}


	public  void test01(String name){
		List<Map<String,Object>> listMap = new ArrayList<>();
		List<Map<String, Object>> variableData = getVariableData("","",name);
		int value = 0;
		for (Map<String, Object> variableDataMap : variableData) {	//获取变量表值
			if(!"总定员".equals(variableDataMap.get("variable_name"))&&
					!"综合楼面积".equals(variableDataMap.get("variable_name"))){
				Map<String, Object> formula = getFormula(variableDataMap.get("variable_name"));//获取公式
				Map<String, Object> assemble = getAssemble(variableDataMap.get("maintain_organization_name"),
						variableDataMap.get("variable_name"),formula.get("constant"),formula.get("expression"));//公式赋值
				int number = getNumber(assemble,formula.get("expression")); //计算
				//赋值
				variableDataMap.put("value", number);
				value+=number;
				listMap.add(variableDataMap);
			}else{//总定员或者综合楼面积,保存当前对象
				variableDataMap.put("value", value);
				listMap.add(variableDataMap);
				value = 0;
			}
			
		}
		list = listMap;
		sysoList();
	} 

}
